package com.kungkhies.persistence.kungkher;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import com.kungkhies.domain.kungkher.Kungkher;
import com.kungkhies.domain.kungkhie.KungkhieSummary;
import java.sql.Timestamp;
import java.util.Date;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.access.annotation.Secured;

public class JdbcSupportKungkherDaoImpl
        extends JdbcDaoSupport implements KungkherDao
{
    private static final String SQL_INSERT_KUNGKHER = "insert into kungkher (username, password, fullname, email, update_by_email) values (?, ?, ?, ?, ?)";
    private static final String SQL_UPDATE_KUNGKHER = "update kungkher set username = ?, password = ?, fullname = ?, set email = ?"
            + "where id = ?";
    private static final String SQL_SELECT_KUNGKHER = ""
            + "select id, username, password, fullname, email from kungkher";
    private static final String SQL_SELECT_KUNGKHER_BY_ID = SQL_SELECT_KUNGKHER
            + " where id=?";
    private static final String SQL_SELECT_ALL_KUNGKHIES = "select * from kungkhie";
    private static final String SQL_SELECT_ALL_KUNGKHERS = "select * from kungkher";
    
    public JdbcSupportKungkherDaoImpl(){}

    //<start id="java_getKungkherById" /> 
    public Kungkher getKungkherById(long id)
    {
        return getJdbcTemplate().queryForObject(
                SQL_SELECT_KUNGKHER_BY_ID,
                new ParameterizedRowMapper<Kungkher>()
                {
                    public Kungkher mapRow(ResultSet rs, int rowNum)
                            throws SQLException
                    {
                        Kungkher kungkher = new Kungkher();
                        kungkher.setId(rs.getLong(1));
                        kungkher.setKungkherName(rs.getString(2));
                        kungkher.setPassword(rs.getString(3));
                        kungkher.setFullName(rs.getString(4));
                        kungkher.setEmail(rs.getString(5));
                        return kungkher;
                    }
                }, id);
    }
    //<end id="java_getKungkherById" />

    //<start id="java_addKungkher" /> 
    @Secured("ROLE_KUNGKHER")
    public void addKungkher(Kungkher kungkher)
    {
        getJdbcTemplate().update(SQL_INSERT_KUNGKHER,
                kungkher.getKungkherName(),
                kungkher.getPassword(),
                kungkher.getFullName(),
                kungkher.getEmail(),
                kungkher.isUpdateByEmail());
        kungkher.setId(queryForIdentity());
    }
    //<end id="java_addKungkher" />

    public void saveKungkher(Kungkher kungkher)
    {
        getJdbcTemplate().update(SQL_UPDATE_KUNGKHER,
                kungkher.getKungkherName(),
                kungkher.getPassword(),
                kungkher.getFullName(),
                kungkher.getEmail(),
                kungkher.getId());
    }

    //<start id="java_queryForIdentity" /> 
    private long queryForIdentity()
    {
        return getJdbcTemplate().queryForLong("call identity()");
    }
    //<end id="java_queryForIdentity" />

    public List<KungkhieSummary> getRecentKungkhies()
    {

        DateTime dt = new DateTime().minusDays(1);

        return getJdbcTemplate().query(SQL_SELECT_ALL_KUNGKHIES,
                new ParameterizedRowMapper<KungkhieSummary>()
                {
                    public KungkhieSummary mapRow(ResultSet rs, int rowNum) throws SQLException
                    {
                        KungkhieSummary kungkhie = new KungkhieSummary();

                        kungkhie.setId(rs.getLong(1));
                        kungkhie.setKungkher(getKungkherById(rs.getLong(2)));
                        kungkhie.setText(rs.getString(3));
                        kungkhie.setWhen(rs.getDate(4));

                        return kungkhie;
                    }
                });


    }

    public void saveKungkhie(KungkhieSummary kungkhie)
    {
        // TODO Auto-generated method stub
    }

    public List<KungkhieSummary> getKungkhiesForKungkher(
            Kungkher kungkher)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public Kungkher getKungkherByUsername(String username)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void deleteKungkhie(long id)
    {
        throw new UnsupportedOperationException();
    }

    public KungkhieSummary getKungkhieById(long id)
    {
        // TODO Auto-generated method stub
        return null;
    }

    public List<Kungkher> findAllKungkhers()
    {
        return getJdbcTemplate().query(SQL_SELECT_ALL_KUNGKHERS,
                new ParameterizedRowMapper<Kungkher>()
                {
                    public Kungkher mapRow(ResultSet rs, int rowNum) throws SQLException
                    {
                        Kungkher kungkher = new Kungkher();

                        kungkher.setId(rs.getLong(1));
                        kungkher.setFullName(rs.getString(2));
                        kungkher.setKungkherName(rs.getString(3));
                        kungkher.setPassword(rs.getString(4));
                        kungkher.setEmail(rs.getString(5));
                        kungkher.setInterests(rs.getString(6));
                        kungkher.setDateRegistered(toDate(rs.getTimestamp(7)));


                        return kungkher;
                    }
                });
    }

    public Date toDate(Timestamp timestamp)
    {
        long milliseconds = timestamp.getTime() + (timestamp.getNanos() / 1000000);
        return new Date(milliseconds);
    }

    @Override
    public boolean kungkherNameExists(String kungkherName)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean emailAddressExists(String emailAddress)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
